import docx
import requests
from bs4 import BeautifulSoup

def SavePicture(url):
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}
    req = requests.get(url, headers=headers)
    with open('picture.jpg', 'wb') as p:
        p.write(req.content)

def main():
    file=docx.Document()
    style = file.styles['Normal']
    style.paragraph_format.first_line_indent = docx.shared.Cm(0.74)
    style.font.name = u'微软雅黑'
    style._element.rPr.rFonts.set(docx.oxml.ns.qn('w:eastAsia'), u'微软雅黑')
    url = "https://huaweicloud.blog.csdn.net/article/details/108484558"
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}
    req = requests.get(url, headers=headers)
    soup = BeautifulSoup(req.text, "lxml")
    content = soup.find("div", attrs={"id":"content_views"})
    for con in content.descendants:
        if con.name == 'p':
            file.add_paragraph(con.text)
        if con.name == 'h1':
            h1 = file.add_heading("", level = 1)
            run1 = h1.add_run(con.text)
            run1.font.name = u'微软雅黑'
            run1._element.rPr.rFonts.set(docx.oxml.ns.qn('w:eastAsia'), u'微软雅黑')
        if con.name == 'h3':
            h2 = file.add_heading("", level = 2)
            run2 = h2.add_run(con.text)
            run2.font.name = u'微软雅黑'
            run2._element.rPr.rFonts.set(docx.oxml.ns.qn('w:eastAsia'), u'微软雅黑')
        if con.name == 'img':
            SavePicture(con['src'])
            file.add_picture("picture.jpg", width=docx.shared.Inches(3))
    file.save("book_list.docx")

if __name__ == '__main__':
    main()
